#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using UTable.Input;
using UTable.Input.MultiTouch;
using UTable.Objects;
using UTable.Objects.Controls;
using System.Windows.Media;
using System.ComponentModel;
using System.IO;
using System.Windows.Media.Imaging;

namespace UTable.Objects
{
    /// <summary>
    /// Interaction logic for UBookPreview.xaml
    /// </summary>
    public partial class UObjectPreview : UObject
    {
        // public virtual UObjectModel Model { get; set; }

        public UObjectPreview()
        {
            InitializeComponent();
			this.Menu.Loaded += new RoutedEventHandler(Menu_Loaded);
        }

		void Menu_Loaded(object sender, RoutedEventArgs e)
		{
			if (!OwnEntity)
			{
				this.Menu.Items.Clear();
			}
            Menu.Radius = 60;
            Menu.Orientation = 15;
			Menu.AddItem("Copy", "Copy");
			// Menu.AddItem("Update", "Update");
            UElementMenuDirectory share = new UElementMenuDirectory();
            share.Radius = 60;
            share.Header = "Share To";
            for (int i = 0; i < UMeeting.WorkspaceCount; i++ )
            {
                share.AddItem("No." + i, "No." + i);
            }
            Menu.Items.Add(share);

			Menu.CommandReceived += new ElementMenuCommandReceivedEventHandler(Menu_CommandReceived);
		}

		void Menu_CommandReceived(UElementMenu sender, ElementMenuCommandReceivedEventArgs args)
		{
			switch (args.Command)
			{
				case "Copy":
					UTableHelper.ShareCenter.CreateCopy(this, ObjectPlacementMode.Top, new Vector(0, -90));
					break;
				case "Share":
					UTableHelper.ShareCenter.CreateShare(this, true, ObjectPlacementMode.Top, new Vector(0, -90));
					break;
				case "Update":
					NotifyModelUpdated("E:/books/book2");
					break;
			}

            if (args.Command.StartsWith("No."))
            {
                int number = int.Parse(args.Command.Substring(3));
                IObject target = UMeeting.WorkspaceList[number];
                int sourceId = -1;
                if (sender.Tag is String)
                {
                    sourceId = int.Parse((sender.Tag as String).Substring(3));
                }
                else
                    return;
                IObject source = UMeeting.WorkspaceList[sourceId];
                int userId = (int)source.QueryData(new QueryWorkspaceOnwerIdMessage());
                Console.WriteLine("Start Sharing to: " + number + ", " + userId + ", " + sourceId);
                target.SendMessage( new ReceiveSharedObjectMessage(this, userId, sourceId));
            }
		}

		public string FolderPath
		{
			get { return (string)GetValue(FolderPathProperty); }
			set { SetValue(FolderPathProperty, value); }
		}

		public static readonly DependencyProperty FolderPathProperty =
			DependencyProperty.Register("FolderPath", typeof(string), typeof(UObjectPreview), new UIPropertyMetadata(String.Empty));



        public override void OnMessageReceived(object message)
        {
            base.OnMessageReceived(message);
        }

		public override object OnDataQuerying(object dataFlag)
		{
			if (dataFlag is String && (dataFlag as String) == "PagesFolderPath")
			{
				return Preview.PagesFolderPath;
			}
            else if (dataFlag is QueryFolderPathMessage)
            {
                return Preview.PagesFolderPath;
            }
            return base.OnDataQuerying(dataFlag); ;
		}

		public override object GetModel()
		{
            return this.FolderPath;
		}

		public override void UpdateModel(object updateMessage)
		{
			this.FolderPath = updateMessage as string;
		}

		public override void InitializeModel(object model)
		{
			this.FolderPath = model as String;
		}

    }
}
